package com.android.kotlincoroutinemvvmdemo.contentprovider

import android.content.ContentValues
import android.database.Cursor
import android.database.sqlite.SQLiteDatabase
import android.net.Uri

class GestureContentProvider : GohiContentProvider() {

    init {
        mUriMatcher.addURI(SqlConstant.AUTHORITY_GESTURE, SqlConstant.TABLE_GESTURE, SqlConstant.GESTURE_DIR)
        mUriMatcher.addURI(SqlConstant.AUTHORITY_GESTURE, "${SqlConstant.TABLE_GESTURE}/#", SqlConstant.GESTURE_ITEM)
    }

    override fun onCreateOfCP() {
        super.onCreateOfCP()
        //注册ContentObserver相关的uri,这样才会回调ContentObserver的onChange()方法
        //boolean notifyForDescendants传false表示单一目录,true表示当前目录及其子目录.

        //注册具体的uri
        this.context!!.contentResolver.registerContentObserver(
            Uri.parse("${SqlConstant.CONTENT_PROVIDER_SCHEME}${SqlConstant.AUTHORITY_GESTURE}/${SqlConstant.TABLE_GESTURE}/${SqlConstant.GESTURE_DIR}"),
            false,
            mDBContentObserver
        )

        //通过#通配后面的int数值
        this.context!!.contentResolver.registerContentObserver(
            Uri.parse("${SqlConstant.CONTENT_PROVIDER_SCHEME}${SqlConstant.AUTHORITY_GESTURE}/${SqlConstant.TABLE_GESTURE}/#"),
            true,
            mDBContentObserver
        )
    }


    override fun getTypeOfCP(uri: Uri): String {
        return when (mUriMatcher.match(uri)) {
            SqlConstant.GESTURE_DIR -> {
                "vnd.android.cursor.dir/vnd.${SqlConstant.AUTHORITY_GESTURE}.${SqlConstant.TABLE_GESTURE}"
            }
            SqlConstant.GESTURE_ITEM -> {
                "vnd.android.cursor.item/vnd.${SqlConstant.AUTHORITY_GESTURE}.${SqlConstant.TABLE_GESTURE}"
            }
            else -> {
                throw IllegalArgumentException(String.format("Unknown URI: %s", uri))
            }
        }
    }

    override fun queryOfCP(
        database: SQLiteDatabase,
        uri: Uri,
        projection: Array<out String>?,
        selection: String?,
        selectionArgs: Array<out String>?,
        sortOrder: String?
    ): Cursor? {
        var cursor: Cursor? = null
        when (mUriMatcher.match(uri)) {
            SqlConstant.GESTURE_DIR -> {
                cursor =
                    database.query(
                        SqlConstant.TABLE_GESTURE,
                        projection,
                        selection,
                        selectionArgs,
                        null,
                        null,
                        sortOrder
                    )
                cursor.setNotificationUri(this.context!!.contentResolver, uri)
            }
            SqlConstant.GESTURE_ITEM -> {
                val segments = uri.pathSegments
                if (segments.size < 2) {
                    return null
                }
                val gestureId = segments[1]
                cursor =
                    database.query(
                        SqlConstant.TABLE_GESTURE,
                        projection,
                        "id=?",
                        arrayOf(gestureId),
                        null,
                        null,
                        sortOrder
                    )
                cursor.setNotificationUri(this.context!!.contentResolver, uri)
            }
        }
        return cursor
    }

    override fun insertOfCP(database: SQLiteDatabase, uri: Uri, values: ContentValues?): Uri? {
        var uriReturn: Uri? = null
        when (mUriMatcher.match(uri)) {
            SqlConstant.GESTURE_DIR,
            SqlConstant.GESTURE_ITEM -> {
                val gesturedId: Long = database.insert(SqlConstant.TABLE_GESTURE, null, values)
                uriReturn = Uri.parse("${SqlConstant.CONTENT_PROVIDER_SCHEME}${SqlConstant.AUTHORITY_GESTURE}/${SqlConstant.TABLE_GESTURE}/${gesturedId}")
                mDBContentObserver.updateDataType(DBContentObserver.TABLE_GESTURE_INSERT)
                this.context!!.contentResolver.notifyChange(uri, mDBContentObserver)
            }
        }
        return uriReturn
    }

    override fun deleteOfCP(
        database: SQLiteDatabase,
        uri: Uri,
        selection: String?,
        selectionArgs: Array<out String>?
    ): Int {
        var deleteRows = 0
        when (mUriMatcher.match(uri)) {
            SqlConstant.GESTURE_DIR -> {
                deleteRows = database.delete(SqlConstant.TABLE_GESTURE, selection, selectionArgs)
                mDBContentObserver.updateDataType(DBContentObserver.TABLE_GESTURE_DELETE_DIR)
                this.context!!.contentResolver.notifyChange(uri, mDBContentObserver)
            }
            SqlConstant.GESTURE_ITEM -> {
                val segments = uri.pathSegments
                if (segments.size < 2) {
                    return 0
                }
                val gestureId = segments[1]
                deleteRows = database.delete(SqlConstant.TABLE_GESTURE, "id=?", arrayOf(gestureId))
                mDBContentObserver.updateDataType(DBContentObserver.TABLE_GESTURE_DELETE_ITEM)
                this.context!!.contentResolver.notifyChange(uri, mDBContentObserver)
            }
        }
        return deleteRows
    }

    override fun updateOfCP(
        database: SQLiteDatabase,
        uri: Uri,
        values: ContentValues?,
        selection: String?,
        selectionArgs: Array<out String>?
    ): Int {
        var updateRows = 0
        when (mUriMatcher.match(uri)) {
            SqlConstant.GESTURE_DIR -> {
                updateRows = database.update(SqlConstant.TABLE_GESTURE, values, selection, selectionArgs)
                mDBContentObserver.updateDataType(DBContentObserver.TABLE_GESTURE_UPDATE_DIR)
                this.context!!.contentResolver.notifyChange(uri, mDBContentObserver)
            }
            SqlConstant.GESTURE_ITEM -> {
                val segments = uri.pathSegments
                if (segments.size < 2) {
                    return 0
                }
                val gestureId = segments[1]
                updateRows = database.update(SqlConstant.TABLE_GESTURE, values, "id=?", arrayOf(gestureId))
                //database.update(SqlConstant.TABLE_GESTURE, values, selection, selectionArgs)
                mDBContentObserver.updateDataType(DBContentObserver.TABLE_GESTURE_UPDATE_ITEM)
                this.context!!.contentResolver.notifyChange(uri, mDBContentObserver)
            }
        }
        return updateRows
    }

}